package com.wl.srb.oss.controller.api;

import com.wl.common.exception.BusinessException;
import com.wl.common.result.R;
import com.wl.common.result.ResponseEnum;
import com.wl.srb.oss.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "阿里云文件管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/api/oss/file")
public class FileController {
    @Autowired
    FileService fileService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public R upload(@RequestParam("file")MultipartFile file,@RequestParam("module")String module){
        String url = null;
        try {
            url = fileService.upload(file.getInputStream(),module,file.getOriginalFilename());
            return R.ok().message("文件上传成功").data("url",url);
        } catch (IOException e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR,e);
        }
    }

    @ApiOperation("删除oss文件")
    @DeleteMapping("/remove")
    public R remove(@RequestParam("url") String url){
        fileService.removeFile(url);
        return R.ok().message("删除成功");
    }
}
